package cn.sys.controller;

import cn.sys.controller.dto.SearchDTO;
import cn.sys.entity.Dormitory;
import cn.sys.entity.ManagerInfo;
import cn.sys.entity.Student;
import cn.sys.service.DormitoryService;
import cn.sys.service.impl.DormitoryServiceImpl;
import cn.sys.util.ParseRequestParameterUtil;
import cn.sys.util.ResponseUtil;
import cn.sys.util.ResultData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/dormitory")
public class DormitoryServlet extends HttpServlet {
    private DormitoryService dormitoryService = new DormitoryServiceImpl();

    @Override
    protected void service(HttpServletRequest req,
                           HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        System.out.println(action);

        switch (action){
            case "getDormitoryInfo" : getDormitoryInfo(req, resp);break;
            case "statisticalDormitory" :statisticalDormitory(req, resp);break;
            case "findDormitoryPosition" : findDormitoryPosition(req, resp);break;
            case "findDormitoryChooseable" : findDormitoryChooseable(req, resp);break;
            case "addStudentToDormitory" : addStudentToDormitory(req,resp);break;
            case "addDormitory" :addDormitory(req, resp);break;
            case "findDormitoryAndPosition" : findDormitoryAndPosition(req, resp);break;
            case "findDormitoryUpdateInfo" : findDormitoryUpdateInfo(req, resp);break;
            case "updateDormitoryInfo" : updateDormitoryInfo(req, resp);break;
            case "findAllDormitoryPositionAndDormitoryInfo":findAllDormitoryPositionAndDormitoryInfo(req,resp);break;
            case "findDormitoryListBySearch" : findDormitoryListBySearch(req, resp);break;
            case "getDetailDormitoryAndStuList" : getDetailDormitoryAndStuList(req,resp);break;
        }
    }

    // 得到寝室详细信息，以及入住学生列表
    private void getDetailDormitoryAndStuList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SearchDTO searchDTO = (SearchDTO) ParseRequestParameterUtil.parseRequestParams(req.getParameterMap(), SearchDTO.class);
        // 根据dorId查询寝室信息，根据dorId查询学生列表
        ResultData resultData = dormitoryService.findDormitoryAndStudentListByDorId(searchDTO);
        ResponseUtil.send(resp, resultData);
    }

    // 查询寝室列表
    private void findDormitoryListBySearch(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SearchDTO searchDTO = (SearchDTO) ParseRequestParameterUtil.parseRequestParams(req.getParameterMap(), SearchDTO.class);
        searchDTO.setOrderBy("dor_id desc");
        ResultData resultData = dormitoryService.findDormitoryListBySearch(searchDTO);
        ResponseUtil.send(resp, resultData);
    }

    // 查询到所有的宿舍信息和宿舍位置信息
    private void findAllDormitoryPositionAndDormitoryInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultData resultData = dormitoryService.findAllDormitoryAndPosition();
        ResponseUtil.send(resp, resultData);
    }

    // 修改寝室信息之前，先把寝室信息查询出来，以便修改其他相关信息
    private void updateDormitoryInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute("managerInfo");
        if(managerInfo == null){
            ResponseUtil.send(resp, ResultData.fail(ResultData.LOGIN_FAIL, "请先登录"));
            return;
        }
        Dormitory dormitory = (Dormitory) ParseRequestParameterUtil.parseRequestParams(req.getParameterMap(), Dormitory.class);
        dormitory.setManagerId(managerInfo.getId());
        ResultData resultData = dormitoryService.updateDormitoryInfo(dormitory);
        ResponseUtil.send(resp, resultData);
    }

    // 根据宿舍id查询一个宿舍信息
    private void findDormitoryUpdateInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer dorId = Integer.valueOf(req.getParameter("dorId").trim());
        ResultData resultData = dormitoryService.findDormitoryByDorId(dorId);
        ResponseUtil.send(resp, resultData);
    }

    // 查询寝室位置以及寝室信息，拼接好后返回给前端：{dorId:dorId, position:apartmentName + floor + doorNum}
    private void findDormitoryAndPosition(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultData resultData = dormitoryService.findDormitoryAndPostions();
        ResponseUtil.send(resp, resultData);
    }

    // 添加宿舍信息
    private void addDormitory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute("managerInfo");
        if(managerInfo == null){
            ResponseUtil.send(resp, ResultData.fail(ResultData.LOGIN_FAIL, "请先登录"));
            return;
        }
        // 获取前端信息
        Dormitory dormitory = (Dormitory) ParseRequestParameterUtil.parseRequestParams(req.getParameterMap(), Dormitory.class);
        // 添加其它默认信息
        dormitory.setCreateTime(new Date());
        dormitory.setSurplusNum(dormitory.getMaxNum());
        dormitory.setState(0);
        dormitory.setManagerId(managerInfo.getId());
        ResultData resultData = dormitoryService.addDormitory(dormitory);
        ResponseUtil.send(resp, resultData);
    }

    // 添加学生到宿舍
    private void addStudentToDormitory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Integer dorId = Integer.valueOf(req.getParameter("dorId"));
        Integer stuId = Integer.valueOf(req.getParameter("stuId"));
        Integer state = Integer.valueOf(req.getParameter("state"));
        ResultData resultData = dormitoryService.addStuToDormitory(dorId, stuId, state);
        ResponseUtil.send(resp, resultData);
    }

    // 查找宿舍楼以及状态对应的宿舍信息
    private void findDormitoryChooseable(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        SearchDTO searchDTO = (SearchDTO) ParseRequestParameterUtil.parseRequestParams(req.getParameterMap(), SearchDTO.class);
        ResultData resultData = dormitoryService.findDormitoryChooseable(searchDTO);
        ResponseUtil.send(resp, resultData);
    }

    // 查找宿舍位置
    private void findDormitoryPosition(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 查询到宿舍位置表的信息，封装在map中返回
        ResultData resultData = dormitoryService.findAllDormitoryPostion();
        ResponseUtil.send(resp, resultData);
    }

    // 宿舍信息统计
    private void statisticalDormitory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        ResultData resultData = dormitoryService.statisticDormitoryInfo();
        ResponseUtil.send(resp, resultData);
    }

    // 查询用户的宿舍信息
    private void getDormitoryInfo(HttpServletRequest req, HttpServletResponse resp) throws IOException{
        HttpSession session = req.getSession();
        // 注：如果没有登录，session获取到的studentInfo为空，不能直接调用getStuId()方法，否则空指针异常
        Student student = (Student) session.getAttribute("studentInfo");
        if(student == null){
            ResponseUtil.send(resp, ResultData.fail(ResultData.OPERATION_FAIL, "请先登录"));
        }else {
            Integer stuId = student.getStuId();
            ResultData resultData = dormitoryService.findDormitoryInfoByStuId(stuId);
            ResponseUtil.send(resp, resultData);
        }
    }
}
